package com.xhh.nphm.gaoxin.controller;

import com.xhh.nphm.common.annotation.Log;
import com.xhh.nphm.common.core.controller.BaseController;
import com.xhh.nphm.common.core.domain.AjaxResult;
import com.xhh.nphm.common.core.domain.entity.SysUser;
import com.xhh.nphm.common.enums.BusinessType;
import com.xhh.nphm.common.utils.ShiroUtils;
import com.xhh.nphm.gaoxin.entity.GxqTwfSendBackEty;
import com.xhh.nphm.gaoxin.entity.GxqTworkFormBaseEty;
import com.xhh.nphm.gaoxin.entity.GxqTworkFormLogEty;
import com.xhh.nphm.gaoxin.service.GxqITworkFormLogService;
import com.xhh.nphm.gaoxin.service.GxqIWorkFormBaseService;
import com.xhh.nphm.gaoxin.service.GxqIWfSendBackService;
import com.xhh.nphm.system.domain.Twfurgent;
import com.xhh.nphm.system.service.ITwfurgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 电话派单催办Controller
 *
 * @date 2022-05-10
 */
@Controller
@RequestMapping("/gaoxin/system/twfurgent")
public class GxqTwfurgentController extends BaseController {
    private String prefix = "system/gaoxin/twfurgent";


    @Autowired
    private ITwfurgentService twfurgentService;
    @Autowired
    private GxqIWfSendBackService gxqWfSendBackService;
    @Autowired
    private GxqITworkFormLogService gxqITworkFormLogService;
    @Autowired
    private GxqIWorkFormBaseService gxqWorkFormBaseService;


    /**
     * 新增电话派单催办
     */
    @GetMapping("/twfurgentAdd/{pkId}")
    public String twfurgentAdd(@PathVariable Integer pkId,ModelMap map) {
        map.put("sendId",pkId);
        return prefix + "/twfurgentAdd";
    }

    /**
     * 新增保存电话派单催办
     */
    @Log(title = "电话派单催办", businessType = BusinessType.INSERT)
    @PostMapping("/twfurgentAddJson")
    @ResponseBody
    public AjaxResult twfurgentAddJson(Twfurgent twfurgent) {
        SysUser user = ShiroUtils.getSysUser();
        GxqTwfSendBackEty GxqTwfSendBackEty = gxqWfSendBackService.getById(twfurgent.getSendId());
        if (GxqTwfSendBackEty !=null) {
            twfurgent.setSdeptId(GxqTwfSendBackEty.getSdeptId());
            twfurgent.setSdeptName(GxqTwfSendBackEty.getSdeptName());
            twfurgent.setSuserName(GxqTwfSendBackEty.getSuserName());
            twfurgent.setSuserNo(GxqTwfSendBackEty.getSuserNo());
            twfurgent.setSbdeptId(GxqTwfSendBackEty.getSbdeptId());
            twfurgent.setSbdeptName(GxqTwfSendBackEty.getSbdeptName());
            twfurgent.setWfId(GxqTwfSendBackEty.getWfId());
            twfurgent.setWorkFormNo(GxqTwfSendBackEty.getMyTwfNO());
            twfurgent.setBackLimit(GxqTwfSendBackEty.getBackLimit());
        }
        GxqTworkFormBaseEty GxqTworkFormBaseEty = gxqWorkFormBaseService.getById(GxqTwfSendBackEty.getWfId());
        if (GxqTworkFormBaseEty !=null) {
            twfurgent.setWorkFormNo(GxqTworkFormBaseEty.getWorkFormNo());
        }
        twfurgent.setMsgTime(new Date());
        int row = 0;
        boolean save = twfurgentService.save(twfurgent);
        if (save) {
            row = 1;
            GxqTworkFormLogEty GxqTworkFormLogEty = new GxqTworkFormLogEty();
            GxqTworkFormLogEty.setOpDeptNo(user.getDeptId().toString());
            GxqTworkFormLogEty.setOpDeptName(user.getDeptName());
            GxqTworkFormLogEty.setOpUserNo(user.getUserNo());
            GxqTworkFormLogEty.setOpUserName(user.getUserName());
            GxqTworkFormLogEty.setEffectFlag(1);
            GxqTworkFormLogEty.setWsOpen(1);
            GxqTworkFormLogEty.setWsDesc("催办");
            GxqTworkFormLogEty.setOpAction("催办");
            GxqTworkFormLogEty.setOpBtnId(-1);
            GxqTworkFormLogEty.setOpDesc("催办");
            gxqITworkFormLogService.save(GxqTworkFormLogEty);
        }
        return toAjax(row);
    }

}
